﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
namespace DataBindingTest
{

    public struct ViewEvent{
        public object sender { get; set; }
        public EventArgs eventInfo { get; set; }
        public ViewEvent(Object o, EventArgs info) : this()
        {
            sender = o;
            eventInfo = info;
        }
    }
    public class MyPresenter : IObserver<ViewEvent>, INotifyPropertyChanged
    {
        IDisposable unsub;
        public ObservableCollection<MyModel> DiveList { get; private set; }
        private NowJumpingDataStruct nowJumping;
        public NowJumpingDataStruct NowJumpingData
        {
            get
            {
                return nowJumping;
            }
            set
            {
                if (!Object.ReferenceEquals(value, this.NowJumpingData))
                {
                    this.nowJumping = value;
                    NotifyPropertyChanged();
                }
            }
        }
        public MyPresenter ()
	    {
            DiveList = new ObservableCollection<MyModel>();
            DiveList.Add(new MyModel("Johan", "Volt", "ASD"));
            DiveList.Add(new MyModel("Kalle", "asd", "ASD"));
            DiveList.Add(new MyModel("Stina", "HEJAWIE", "ASD"));
            DiveList.Add(new MyModel("Eva", "TJO", "ASD"));
            NowJumpingData = new NowJumpingDataStruct("Kalle", "Dubbel bakåtvolt", "Finland");
        }
        public MyPresenter(MainWindow main)
        {
            DiveList = new ObservableCollection<MyModel>();
            DiveList.Add(new MyModel("Johan", "Volt", "ASD"));
            DiveList.Add(new MyModel("Kalle", "asd", "ASD"));
            DiveList.Add(new MyModel("Stina", "HEJAWIE", "ASD"));
            DiveList.Add(new MyModel("Eva", "TJO", "ASD"));
            NowJumpingData = new NowJumpingDataStruct("Kalle", "Dubbel bakåtvolt", "Finland");
            unsub = main.Subscribe(this);
        }




        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public void OnCompleted()
        {
            throw new NotImplementedException();
        }

        public void OnError(Exception error)
        {
            throw new NotImplementedException();
        }

        public void OnNext(ViewEvent value)
        {
            if (value.sender.GetType() == typeof(Button)){
                Button b = (Button) value.sender;
                switch (b.Name)
                {
                    case "btn_add":
                        for (int i = 0; i < 5; i++)
		                {
                            Random r = new Random();
                            String n = "";
                            String nat = "";
                            String type= "";
                            for (int j = 0; j < 15; j++)
                            {
                                char c = (char) r.Next(33,127);
                                n += c;
                                c = (char) r.Next(33,127);
                                nat += c;
                                c = (char) r.Next(33,127);
                                type += c;
                            }
                            DiveList.Add(new MyModel(n,type,nat));
			            }
                        break;
                    case "btn_clear":
                        DiveList.Clear();
                        break;

                    default:
                        break;
                }
            }
        }
    }

    public struct NowJumpingDataStruct  : IComparable<NowJumpingDataStruct>
    {
        //member variables
        public String Name { get; set; }
        public String Type { get; set; }
        public String Nationality { get; set; }

        public NowJumpingDataStruct(String n, String t, String nat)
            : this()
        {
            Name = n;
            Type = t;
            Nationality = nat;
        }

        public NowJumpingDataStruct(MyModel m)
            : this()
        {
            Name = m.name;
            Type = m.type;
            Nationality = m.nationality;
        }
    
        public int CompareTo(NowJumpingDataStruct otherObj)
        {
            int comp = 0;
            comp += this.Name.CompareTo(otherObj.Name);
            if (comp == 0)
            {
                comp += this.Nationality.CompareTo(otherObj.Nationality);
                return comp;
            }
            return comp;
        }
        
    }
}
